<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="昵称/手机号">
            <a-input v-decorator="['search']" placeholder="请输入昵称/手机号"/>
          </a-form-item>
          <a-form-item label="注册时间">
            <a-range-picker v-decorator="['between']" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD HH:mm:ss"/>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>
    </div>
    <s-table ref="table" :columns="columns" :data="loadData" :loading="isLoading" :pageSize="15" rowKey="uid">
      <!-- 客户头像 -->
      <template v-slot:avatar="text">
        <div class="avatar">
          <img v-if="text" width="45" height="45" :src="text" alt="用户头像"/>
          <img v-else width="45" height="45" src="~@/assets/img/default-avatar.png" alt="用户头像"/>
        </div>
      </template>
      <template v-slot:info="item">
        <p>{{ item.username }}</p>
        <p class="c-p">{{ item.mobile }}</p>
      </template>
      <!-- 余额/积分 -->
      <template v-slot:balance="text, item">
        <p>
          <span>余额：</span>
          <span class="c-p">{{ text }}</span>
        </p>
        <p>
          <span>积分：</span>
          <span class="c-p">{{ item.points | fixed }}</span>
        </p>
      </template>
      <!-- 注册来源 -->
      <template v-slot:registerSource="text">
        <div class="platform">
          <platform-icon :iconSize="17" :name="text" :showTips="true"/>
        </div>
      </template>
      <!-- 操作 -->
      <template v-slot:action="item">
        <div class="actions">
          <a
              v-if="$module('market-recharge')"
              v-action:recharge
              title="客户充值"
              @click="handleRecharge(item)"
          >
            充值
          </a>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </div>
      </template>
    </s-table>
    <RechargeForm ref="RechargeForm" @handleSubmit="handleRefresh"/>
    <PointsForm ref="PointsForm" @handleSubmit="handleRefresh"/>
  </a-card>
</template>

<script>
import {filterModules} from '@/core/app'
import * as Api from '@/api/user'
import {STable} from '@/components'
import PlatformIcon from '@/components/PlatformIcon'
import {GradeForm, RechargeForm} from './modules'
import PointsForm from "@/views/market/points/PointsForm.vue";

// table表头
const columns = filterModules([
  {
    title: '客户编码',
    dataIndex: 'uid'
  },
  {
    title: '客户头像',
    dataIndex: 'avatar',
    scopedSlots: {customRender: 'avatar'}
  },
  {
    title: '昵称/手机号',
    scopedSlots: {customRender: 'info'}
  },
  {
    title: '余额/积分',
    dataIndex: 'balance',
    scopedSlots: {customRender: 'balance'}
  },
  {
    title: '注册来源',
    dataIndex: 'registerSource',
    scopedSlots: {customRender: 'registerSource'}
  },
  {
    title: '注册时间',
    dataIndex: 'creationTime'
  },
  {
    title: '操作',
    width: '180px',
    scopedSlots: {customRender: 'action'}
  }
])

export default {
  name: 'Index',
  components: {
    STable,
    GradeForm,
    RechargeForm,
    PointsForm,
    PlatformIcon
  },
  data() {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // table表头
      columns,
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({...param, ...this.queryParam})
            .then(response => response)
      }
    }
  },
  created() {
  },
  methods: {
    // 客户充值
    handleRecharge(item) {
      // this.$refs.RechargeForm.handle(item)
      this.$refs.PointsForm.handle(item)
    },

    // 删除记录
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleted({userId: item.uid})
              .then(result => {
                app.$message.success('操作成功', 1.5)
                app.handleRefresh()
              })
              .finally(result => modal.destroy())
        }
      })
    },

    // 确认搜索
    handleSearch(e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = {...this.queryParam, ...values}
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 刷新列表
     * @param {Boolean} bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    }

  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
